const FormatSpaceString = require('../common/FormatSpaceString');

const gender_enum = [ '男', '女', '未知' ];
const grade_enum = [ '大一', '大二', '大三', '大四', '研一', '研二', '研三' ];

class UserInfo {
    constructor(obj) {
        this.username = obj.name;
        this.gender = obj.gender;
        this.phone = obj.phone;
        this.email = obj.email;
        this.bio = obj.bio;
        this.school = obj.school;
        this.major = obj.major;
        this.grade = obj.grade;
    }
    check() {
        let flag;
        // 用户名处理
        if (this.username) {
            let username = new FormatSpaceString(this, 'username', false, false, null, null, null, 50);
            flag = username.check();
            if (!flag.success) {
                return flag;
            }
            this.username = username.OldValue;
        }
        // 性别
        let gender = this.gender;
        if (gender && gender_enum.indexOf(gender) === -1) {
            return { success: false, msg: '性别错误' };
        }
        // 手机号
        if (this.phone) {
            let phone = new FormatSpaceString(this, 'phone', false, false, null, null, null, 20);
            flag = phone.check();
            if (!flag.success) {
                return flag;
            }
            this.phone = phone.value;
        }
        // 邮箱
        if (this.email) {
            let email = new FormatSpaceString(this, 'email', false, false, null, null, null, 50);
            flag = email.check();
            if (!flag.success) {
                return flag;
            }
            this.email = email.value;
        }
        // 个人简介
        if (this.bio) {
            let bio = new FormatSpaceString(this, 'bio', false, false, null, null, null, 500);
            flag = bio.check();
            if (!flag.success) {
                return flag;
            }
            this.bio = bio.OldValue;
        }
        // 学校
        if (this.school) {
            let school = new FormatSpaceString(this, 'school', false, false, null, null, null, 50);
            flag = school.check();
            if (!flag.success) {
                return flag;
            }
            this.school = school.value;
        }
        // 专业
        if (this.major) {
            let major = new FormatSpaceString(this, 'major', false, false, null, null, null, 50);
            flag = major.check();
            if (!flag.success) {
                return flag;
            }
            this.major = major.value;
        }
        // 年级
        let grade = this.grade;
        if (grade && grade_enum.indexOf(grade) === -1) {
            return { success: false, msg: '年级错误' };
        }
        return { success: true };
    }
}

module.exports = UserInfo;